package com.lby.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.lby.model.ScoreDimensionConfig;

@Repository
public class MyBeanDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    
	@Qualifier("readJdbcTemplate")
	@Autowired
	private JdbcTemplate jdbcTemplateReadOnly;
	
	public Map<String, Object> getRepacket(long id) {
		String sql = "SELECT * FROM ubi_receive_record WHERE id = ?";
		Map<String, Object>  map = jdbcTemplateReadOnly.queryForMap(sql, id);
		return map;
	}
	
	public int update(long userId, int bind) {
		String sql = "update ubi_receive_record set obd_bind = ?, update_time = ? where user_id = ?";
        return jdbcTemplate.update(sql, bind, new Date(), userId);
	}
	
	public List<Map<String, Object>> listAllConfig() {
		String sql = "SELECT * FROM score_dimension_config";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}
	
	public ScoreDimensionConfig getConfigById(long id) {
		String sql = "SELECT * FROM score_dimension_config where id = ?";
		Map<String, Object> map = jdbcTemplate.queryForMap(sql, id);
		
		ScoreDimensionConfig config = new ScoreDimensionConfig();
		config.setDimensionName(String.valueOf(map.get("dimension_name")));
		config.setId(Long.parseLong(map.get("id") + ""));
		Boolean b = (Boolean) map.get("init");
		if(b) {
			config.setInit(1);	
		} else {
			config.setInit(0);
		}
		
		config.setRatio(Float.parseFloat(String.valueOf(map.get("ratio"))));
		config.setScoreHigh(Float.parseFloat(String.valueOf(map.get("score_high"))));
		config.setScoreLow(Float.parseFloat(String.valueOf(map.get("score_low"))));
		
		return config;
	}
}
